package com.test.baoxiudemo.battery;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

public class BatteryInfoDBHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "batteryinfo";
	private static final String TABLE_NAME = DATABASE_NAME;
	private static final int DATABASE_VERSION = 1;

	private static BatteryInfoDBHelper instance;

	private BatteryInfoDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	private BatteryInfoDBHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	public static BatteryInfoDBHelper getInstance(Context context) {
		if (instance == null) {
			synchronized (BatteryInfoDBHelper.class) {
				if (instance == null) {
					instance = new BatteryInfoDBHelper(context);
				}
			}
		}
		return instance;
	}

	public synchronized boolean add(BatteryInfo info) {
		if (info == null)
			return false;
		boolean result = false;
		ContentValues values = creatValuesFromObject(info);
		SQLiteDatabase db = getWritableDatabase();
		long insert = db.insert(TABLE_NAME, null, values);
		if (insert > 0) {
			result = true;
		}
		closeDB(db);
		return result;
	}

	public synchronized BatteryInfo getLastInfo() {
		BatteryInfo result = null;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from "+TABLE_NAME+" where time in (select max(time) from " + TABLE_NAME+ " as maxtime)", null);
		if (cursor != null && cursor.getCount() > 0) {
			if (cursor.moveToFirst()) {
				result = creatObjectFromCursor(cursor);
			}
		}
		closeCursor(cursor);
		closeDB(db);
		return result;
	}

	public synchronized boolean needAdd(BatteryInfo info) {
		if(info == null){
			return false;
		}
		boolean result = false;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from "+TABLE_NAME+" where time in (select max(time) from " + TABLE_NAME+ " as maxtime)", null);
		if (cursor != null && cursor.getCount() > 0) {
			if (cursor.moveToFirst()) {
				int level = cursor.getInt(cursor.getColumnIndex("level"));
				if(info.level != level){
					result = true;
				} else {
					result = false;
				}
			}
		} else {
			result = true;
		}
		closeCursor(cursor);
		closeDB(db);
		return result;
	}
	
	public synchronized ArrayList<BatteryInfo> getAllInfos() {
		ArrayList<BatteryInfo> result = null;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from " + TABLE_NAME
				+ " order by time asc", null);
		if (cursor != null && cursor.getCount() > 0) {
			result = new ArrayList<BatteryInfo>();
			while (cursor.moveToNext()) {
				BatteryInfo info = creatObjectFromCursor(cursor);
				result.add(info);
			}
		}
		closeCursor(cursor);
		closeDB(db);
		return result;
	}

	private void closeCursor(Cursor cursor) {
		if (cursor != null) {
			cursor.close();
		}
	}

	private void closeDB(SQLiteDatabase db) {
		if (db != null) {
			db.close();
		}
	}

	private BatteryInfo creatObjectFromCursor(Cursor cursor) {
		BatteryInfo result = null;
		if (cursor != null) {
			result = new BatteryInfo();
			result.id = cursor.getInt(cursor.getColumnIndex("_id"));
			result.time = cursor.getLong(cursor.getColumnIndex("time"));
			result.level = cursor.getInt(cursor.getColumnIndex("level"));
			result.info = cursor.getString(cursor.getColumnIndex("info"));
		}
		return result;
	}

	private ContentValues creatValuesFromObject(BatteryInfo info) {
		ContentValues result = null;
		if (info != null) {
			result = new ContentValues();
			if (info.time > 0) {
				result.put("time", info.time);
			}
			if (info.level > 0) {
				result.put("level", info.level);
			}
			if (!TextUtils.isEmpty(info.info)) {
				result.put("info", info.info);
			}
		}
		return result;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS "
				+ TABLE_NAME
				+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT, time LONG, level INTEGER, info TEXT)");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

}
